package com.bo.sina.weibo.access;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.bo.WeiboConfig;

public class AccessToken {
	public  static  Map<String,AccessInfo> accessMap=new HashMap<String,AccessInfo>(); 
	
	public  void  initAccessToken(){
		BufferedReader in = null;
		String currLine = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(WeiboConfig.getValue("accessTokenFile").trim()), "UTF-8");   
			in = new BufferedReader(isr);
			while ((currLine = in.readLine()) != null) {
				if (currLine.trim().equals("")) {
					continue;
				}
				String []array=currLine.split("&&");
//				if(array[0].equals(username)){
//					return array[1];
//				}
				AccessInfo accessinfo=new AccessInfo(array[0],array[3],array[1],array[2]);
				this.accessMap.put(array[3],accessinfo);
			}	
		}catch(Exception io){
			io.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException ex) {				
			}
			in = null;
		}
	}
	
	public static String searchAccessToken(String username){
		BufferedReader in = null;
		String currLine = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(WeiboConfig.getValue("accessTokenFile").trim()), "UTF-8");   
			in = new BufferedReader(isr);
			while ((currLine = in.readLine()) != null) {
				if (currLine.trim().equals("")) {
					continue;
				}
				String []array=currLine.split("&&");
				if(array[0].equals(username)){
					return array[1];
				}
			}	
		}catch(Exception io){
			io.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException ex) {				
			}
			in = null;
		}
		return null;
	}
	
	

	public static void main(String args[]){
		AccessToken accessToken=new AccessToken();
		accessToken.initAccessToken();
	}
}
